﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(MaterialPropReplacer))]
public class MaterialPropReplacerEditor : E_ShowButtons<MaterialPropReplacer> { }
#endif
[ExecuteInEditMode]
public class MaterialPropReplacer : MonoBehaviour
{
    public string fromName = "_Color";
    public string toName = "_BaseColor";
    public Material[] mats;
    public bool update;
    public Shader shader;

    [ShowButton]
    void ApplyValue()
    {
        //foreach (var r in GetComponentsInChildren<Renderer>(true))
        //{
        //    try
        //    {
        //        foreach (var mat in r.sharedMaterials)
        //        {
        //            mat.SetColor(toName, mat.GetColor(fromName));
        //        }
        //    }
        //    catch { }
        //}
        foreach (var mat in mats)
        {
            var color = mat.GetColor(fromName);
            if (color != new Color())
                mat.SetColor(toName, color);
        }
    }
    [ShowButton]
    void SetShader()
    {
        foreach (var mat in mats)
        {
            mat.shader = shader;
        }
    }
    void Update()
    {
        if (update) ApplyValue();
    }
}
